package znbkListNtxl.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lancooznbk.R;
import znbkListNtxl.entity.TrainingDifficultProblemsListEntity;
import znbkListNtxl.presenter.TrainingDifficultProblemsListPresenter;
import com.example.lancooznbk.view.adapter.PopupMenuType1Adapter;
import znbkListNtxl.adapter.TrainingDifficultProblemsListAdapter;
import com.example.lancooznbk.view.base.BaseMainFragment;
import com.example.lancooznbk.view.base.ILoadMoreDataView;
import com.example.lancooznbk.view.widget.CustomFooterView;
import com.example.lancooznbk.view.widget.PopupMenu;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018\2\1 0001.
 */

public class TrainingDifficultProblemsListFragment extends BaseMainFragment<TrainingDifficultProblemsListPresenter>
        implements View.OnClickListener,ILoadMoreDataView<TrainingDifficultProblemsListEntity> {


    private TrainingDifficultProblemsListAdapter mTrainingDifficultProblemsListAdapter;

    private TextView mTvSpStatus;
    private TextView mTvSpQuesType;
    private TextView mTvTotal;
    private PopupMenuType1Adapter popupMenuStatusAdapter;
    private PopupMenu statusPopupMenu;
    private PopupMenuType1Adapter popupMenuQuesTypeAdapter;
    private PopupMenu quesTypePopupMenu;
    private View masker;
    private RelativeLayout mSpLayout;

    private Map<String,Integer> mQuesStatusMap = new HashMap<>();
    private Map<String,String> mQuesTypeMap = new HashMap<>();

    private int checkPosition = -1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void updateViews(boolean isShowLoading) {
        mPresenter.getData(isShowLoading);
    }

    @Override
    public int attachLayoutRes() {
        return R.layout.fragment_list_training_difficult_problems;
    }

    @Override
    public void initBaseMainViews(View view, Bundle savedInstanceState) {

        checkPosition = 0;

        mPresenter = new TrainingDifficultProblemsListPresenter(this);

        Log.e("==wgl===","initBaseMainViews");

        mSpLayout = (RelativeLayout) view.findViewById(R.id.rel_toolbar_head);
        mTvSpStatus = (TextView) view.findViewById(R.id.sp_status);
        mTvSpQuesType = (TextView) view.findViewById(R.id.sp_quesType);
        mTvTotal = (TextView) view.findViewById(R.id.tv_paperNumber);
        masker = view.findViewById(R.id.bg_masker);

        View toolBarLayout = view.findViewById(R.id.rel_toolbar_layout);
        ((TextView)toolBarLayout.findViewById(R.id.tv_toolbar_title)).setText(R.string.menu_ntxl);
        ((LinearLayout)toolBarLayout.findViewById(R.id.layout_toolbar_back)).setOnClickListener(this);

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) toolBarLayout.getLayoutParams();
        layoutParams.setMargins(0,getStatusBarHeight(),0,0);

        /*
        * 资源类型
        * */
        mTvSpQuesType.setOnClickListener(this);

        /*
        * 作答状态
        *
        * */
        String[] quesStatusArrays = getResources().getStringArray(R.array.QuesStatusArraysNtxl);
        String[] quesStatusName = new String[quesStatusArrays.length];
        for (int i = 0 ; i < quesStatusArrays.length ; i++){
            String[] split = quesStatusArrays[i].split("\\|");
            quesStatusName[i] = split[0];
            mQuesStatusMap.put(split[0], Integer.valueOf(split[1]));
        }

        popupMenuStatusAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesStatusName);
        statusPopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        statusPopupMenu.setListAdapter(popupMenuStatusAdapter);
        statusPopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                mTvSpStatus.setText(statusPopupMenu.getCheckedStr());
//                mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
                checkPosition = 0;
                mTvSpQuesType.setText("全部题型");

                mXRefreshView.startRefresh();
                mPresenter.getDataByStatus(mQuesStatusMap.get(statusPopupMenu.getCheckedStr()));
            }
        });
        mTvSpStatus.setText(statusPopupMenu.getCheckedStr());
        mTvSpStatus.setOnClickListener(this);
        statusPopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpStatus.setSelected(false);
            }
        });


        mTrainingDifficultProblemsListAdapter = new TrainingDifficultProblemsListAdapter(getActivity());

        mTrainingDifficultProblemsListAdapter.setCustomLoadMoreView(new CustomFooterView(getActivity()));
        mRecyclerView.setAdapter(mTrainingDifficultProblemsListAdapter);
        mTrainingDifficultProblemsListAdapter.setOnItemClickListener(new TrainingDifficultProblemsListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, List<TrainingDifficultProblemsListEntity.QuesItemListBean> list, int position) {
                mPresenter.openItemDetail(position,list.get(position));
            }
        });
    }


    @Override
    public void showNetError() {
        super.showNetError();
        String[] quesTypeNames = new String[1];
        String[] quesTypeTypeIDs = new String[1];

        quesTypeNames[0] = "全部题型";
        quesTypeTypeIDs[0] = "";

        mQuesTypeMap.put(quesTypeNames[0],quesTypeTypeIDs[0]);

        popupMenuQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames);
        quesTypePopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        quesTypePopupMenu.setListAdapter(popupMenuQuesTypeAdapter);
        quesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                Log.e("===showNetError====","quesTypePopupMenu.getCheckedStr()===="+quesTypePopupMenu.getCheckedStr());
                mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(quesTypePopupMenu.getCheckedStr()));
            }
        });
        mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
        quesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpQuesType.setSelected(false);
            }
        });
        setPaperTotal(0);
    }


    @Override
    public void showNoData(String msg) {
        super.showNoData(msg);
        String[] quesTypeNames = new String[1];
        String[] quesTypeTypeIDs = new String[1];

        quesTypeNames[0] = "全部题型";
        quesTypeTypeIDs[0] = "";

        mQuesTypeMap.put(quesTypeNames[0],quesTypeTypeIDs[0]);

        popupMenuQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames);
        quesTypePopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        quesTypePopupMenu.setListAdapter(popupMenuQuesTypeAdapter);
        quesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                Log.e("===showNoData====","quesTypePopupMenu.getCheckedStr()===="+quesTypePopupMenu.getCheckedStr());
                mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(quesTypePopupMenu.getCheckedStr()));
            }
        });
        mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
        quesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpQuesType.setSelected(false);
            }
        });
        setPaperTotal(0);
    }


    @Override
    public void loadData(TrainingDifficultProblemsListEntity data) {

        Log.e("==wgl===","==loadData=="+data.getTotalQues());

        if(mRecyclerView.getLayoutManager() != null ) {
            mRecyclerView.getViewTreeObserver().addOnGlobalLayoutListener(layoutListener);
        }


        String[] quesTypeNames = new String[data.getExistQuesType().size()+1];
        String[] quesTypeTypeIDs = new String[data.getExistQuesType().size()+1];

        for (int i = 0 ; i < data.getExistQuesType().size()+1 ; i++){
            if(i==0){
                quesTypeNames[i] = "全部题型";
                quesTypeTypeIDs[i] = "";
            }else {
                quesTypeNames[i] = data.getExistQuesType().get(i-1).getTypeName();
                quesTypeTypeIDs[i] = data.getExistQuesType().get(i-1).getTypeID();
            }
            mQuesTypeMap.put(quesTypeNames[i],quesTypeTypeIDs[i]);
        }


        popupMenuQuesTypeAdapter = new PopupMenuType1Adapter(getHolderActivity(),quesTypeNames,checkPosition);
        quesTypePopupMenu = new PopupMenu(getHolderActivity(), LinearLayout.LayoutParams.MATCH_PARENT,R.drawable.bg_popupmenu);
        quesTypePopupMenu.setListAdapter(popupMenuQuesTypeAdapter);
        quesTypePopupMenu.setOnItemClickListener(new PopupMenu.OnItemClickListener() {
            @Override
            public void onItemClick(View item, int position, long id) {
                checkPosition = position;
                mTvSpQuesType.setText(quesTypePopupMenu.getCheckedStr());
                mXRefreshView.startRefresh();
                mPresenter.getDataByQuesType(mQuesTypeMap.get(quesTypePopupMenu.getCheckedStr()));
            }
        });

        quesTypePopupMenu.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                masker.setVisibility(View.GONE);
                mTvSpQuesType.setSelected(false);
            }
        });


        Log.e("==count===",""+data.getTotalQues());

        setPaperTotal(data.getTotalQues());
        mTrainingDifficultProblemsListAdapter.clearAndAdd(data.getQuesItemList());
        loadComplete(mTrainingDifficultProblemsListAdapter.getAdapterItemCount() >= data.getTotalQues());
    }

    @Override
    public void loadMoreData(TrainingDifficultProblemsListEntity data) {
        setPaperTotal(data.getTotalQues());
        mTrainingDifficultProblemsListAdapter.add(data.getQuesItemList());
        Log.e("=getAdapterItemCount=","====="+ mTrainingDifficultProblemsListAdapter.getAdapterItemCount());
        Log.e("=getTotalQues=","===="+data.getTotalQues());
        loadComplete(mTrainingDifficultProblemsListAdapter.getAdapterItemCount() >= data.getTotalQues());
    }



    /**
     * 没有试卷或者加载失败的逻辑处理
     */
    private void setPaperTotal(int number) {
        Log.e("==setPaperTotal===",""+number);
        String paperTotal = String.format(getString(R.string.znbkQuesTotal),number);
        mTvTotal.setText(Html.fromHtml(paperTotal));
    }



  /*  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_main,menu);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.item_other_system){
            //ToastUtils.showShort("难点进度提示");
            startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
            return true;
        }
        return false;
    }*/

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.sp_status:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpStatus.setSelected(true);
                statusPopupMenu.showLocation(mSpLayout, 0, -2);
                break;
            case R.id.sp_quesType:
                masker.setVisibility(View.VISIBLE);//阴影布局可见
                mTvSpQuesType.setSelected(true);
                quesTypePopupMenu.showLocation(mSpLayout, 0, -2);
                break;

            case R.id.layout_toolbar_back:
                getHolderActivity().finish();
                break;
//            case R.id.img_main_menu:
//                startActivity(new Intent(getHolderActivity(), ReviewProcessActivity.class));
//                break;
         /* case R.id.fab_coping:
              BaseReviewWrongActivity.launch(v.getContext(),1);
              break;*/
        }
    }



}